Shader "Study/Chapter6/RampTexture"
{
    Properties
    {
        _Color ("Color Tint", Color) = (1, 1, 1, 1)
		_RampTex ("Ramp Tex", 2D) = "white" {}
		[Toggle]_UseRamp("UseRamp",int) = 1
		_Specular ("Specular", Color) = (1, 1, 1, 1)
		_Gloss ("Gloss", Range(8.0, 256)) = 20
		
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
			#pragma multi_compile _ _USERAMP_ON

            #include "UnityCG.cginc"
			#include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
				float3 normal : NORMAL;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
				float3 worldNormal : TEXCOORD0;
				float3 worldPos : TEXCOORD1;
            };

            sampler2D _RampTex;
			float4 _Color;
			float4 _Specular;
			float _Gloss;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld,v.vertex);

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
				fixed3 N = normalize(i.worldNormal);
				fixed3 L = normalize(_WorldSpaceLightPos0.xyz);
				fixed3 V = normalize(UnityWorldSpaceLightDir(i.worldPos));
				fixed NdotL  = dot(N,L) * 0.5 + 0.5;
				fixed3 H = normalize(L + V);   //半角向量 
			
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; 

				fixed3 diffuseColor;
				#if _USERAMP_ON
					diffuseColor = tex2D(_RampTex,fixed2(NdotL ,NdotL )).rgb * _Color.rgb;
				#else
					diffuseColor = NdotL * _Color.rgb;
				#endif
				return fixed4(diffuseColor,1);
				fixed3 diffuse = diffuseColor * _LightColor0.rgb;

				fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,dot(V,H)),_Gloss);

				fixed3 finialColor = ambient + diffuse + specular;
                return fixed4(finialColor,1);
            }
            ENDCG
        }
    }
}
